const { DataTypes } = require('sequelize');
const sequelize = require('../../config/database');

const ExchangeRecord = sequelize.define('ExchangeRecord', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  student_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'students',
      key: 'id'
    }
  },
  prize_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'prizes',
      key: 'id'
    }
  },
  exchange_time: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    allowNull: false
  },
  status: {
    type: DataTypes.ENUM('pending', 'completed', 'cancelled'),
    defaultValue: 'pending',
    allowNull: false
  },
  points_used: {
    type: DataTypes.INTEGER,
    allowNull: false
  }
}, {
  tableName: 'exchange_records',
  timestamps: false
});


// 定义关联关系
ExchangeRecord.associate = function(models) {
  ExchangeRecord.belongsTo(models.student, {
    foreignKey: 'student_id',
    as: 'student'
  });
  
  ExchangeRecord.belongsTo(models.prizes, {
    foreignKey: 'prize_id',
    as: 'prize'
  });
};

module.exports = ExchangeRecord;